package cn.yeziji.forum.bo.user;

import cn.yeziji.forum.utils.StrUtils;
import cn.yeziji.forum.utils.TimeUtils;
import cn.yeziji.forum.vo.user.UserMsgVO;
import cn.yzj.common.CommonSymbol;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户信息BO对象
 *
 * @author gzkemays
 * @since 2022/11/26 11:41 AM
 */
@Data
public class UserMsgBO {
  Integer id;
  String nickname;
  String avatar;
  String desc;
  String level;
  String fansIds;
  String focusIds;
  Date createTime;
  Integer questionCount;
  Integer answerCount;
  Integer fileCount;
  Integer detailCount;

  /**
   * 构造自身的 MsgVo 信息
   *
   * @return 用户信息视图对象
   */
  public UserMsgVO buildSelfUserMsgVO() {
    UserMsgVO vo = new UserMsgVO();
    vo.setNickname(nickname);
    vo.setAvatar(avatar);
    vo.setDesc(desc);
    vo.setLevel(level);
    if (StringUtils.isNotBlank(fansIds)) {
      vo.setFansCount(fansIds.split(CommonSymbol.COMMA).length);
    } else {
      vo.setFansCount(0);
    }
    if (StringUtils.isNotBlank(focusIds)) {
      vo.setFocusCount(focusIds.split(CommonSymbol.COMMA).length);
    } else {
      vo.setFocusCount(0);
    }
    vo.setRegisterTime(StrUtils.dateToStr(TimeUtils.COMMON_SIMPLE_PATTERN, createTime));
    vo.setQuestionCount(questionCount);
    vo.setAnswerCount(answerCount);
    vo.setFileCount(fileCount);
    vo.setDetailCount(detailCount);
    vo.setFocus(false);
    vo.setFans(false);
    return vo;
  }

  /**
   * 根据 token 解析的 userId 访问其他用户时构造的 MsgVo 信息
   *
   * @param userId 请求的用户 id
   * @return 用户信息视图对象
   */
  public UserMsgVO buildUserMsgVO(Integer userId) {
    boolean isFans = false, isFocus = false;
    UserMsgVO vo = new UserMsgVO();
    vo.setNickname(nickname);
    vo.setAvatar(avatar);
    vo.setDesc(desc);
    vo.setLevel(level);
    if (StringUtils.isNotBlank(fansIds)) {
      // 查看 userId 是否在当前用户的粉丝列表当中
      String[] fansSplit = fansIds.split(CommonSymbol.COMMA);
      for (String fansId : fansSplit) {
        if (fansId.equals(String.valueOf(userId))) {
          isFans = true;
          break;
        }
      }
      vo.setFans(isFans);
      vo.setFansCount(fansSplit.length);
    } else {
      vo.setFansCount(0);
    }
    if (StringUtils.isNotBlank(focusIds)) {
      // 查看 userId 是否在当前用户的关注列表当中
      String[] focusSplit = focusIds.split(CommonSymbol.COMMA);
      for (String focusId : focusSplit) {
        if (focusId.equals(String.valueOf(userId))) {
          isFocus = true;
          break;
        }
      }
      vo.setFocus(isFocus);
      vo.setFocusCount(focusSplit.length);
    } else {
      vo.setFocusCount(0);
    }
    vo.setRegisterTime(StrUtils.dateToStr(TimeUtils.COMMON_SIMPLE_PATTERN, createTime));
    vo.setQuestionCount(questionCount);
    vo.setAnswerCount(answerCount);
    vo.setFileCount(fileCount);
    vo.setDetailCount(detailCount);
    return vo;
  }

  public static List<UserMsgVO> buildUserMsgListVo(List<UserMsgBO> list, Integer userId) {
    List<UserMsgVO> vos = new ArrayList<>();
    for (UserMsgBO userMsgBO : list) {
      vos.add(userMsgBO.buildUserMsgVO(userId));
    }
    return vos;
  }
}
